package com.lagou.gateway.filter;

import com.lagou.gateway.constants.GatewayKnownHeader;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@ConditionalOnProperty(prefix = "lagou.gateway.out.standard", name = "enable", havingValue = "true")
public class RemovePrivateRequestHeaderFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest()
                .mutate()
                .headers(
                        headers -> GatewayKnownHeader.PRIVATE_HEADER_NAMES.forEach(headers::remove)
                )
                .build();
        return chain.filter(exchange.mutate().request(request).build());
    }

    @Override
    public int getOrder() {
        return FilterOrder.REMOVE_PRIVATE_HEADER_FILTER_ORDER;
    }
}
